package oss

import (
	"douyin/setup/settings"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"go.uber.org/zap"
	"mime/multipart"
)

// UploadAliyunOss 将文件上传到 阿里云 oss
func UploadAliyunOss(file *multipart.FileHeader, saveFilePath string) (playUrl string, err error) {
	//1.设置oss初始化
	client, err := oss.New( //默认开启CRC数据校验
		settings.Conf.OssConfig.Endpoint,
		settings.Conf.OssConfig.AccessKeyID,
		settings.Conf.OssConfig.AccessKeySecret)
	if err != nil {
		zap.L().Error("oss.UploadAliyunOss oss New Error", zap.Error(err))
	}
	// 2.指定bucket
	bucket, err := client.Bucket(settings.Conf.OssConfig.Bucket)
	if err != nil {
		zap.L().Error("oss.UploadAliyunOss Bucket Error", zap.Error(err))
	}
	// 3.打开文件流
	src, err := file.Open()
	if err != nil {
		zap.L().Error("oss.UploadAliyunOss Bucket fileOpen Error", zap.Error(err))
	}
	defer src.Close()
	// 4.上传文件
	err = bucket.PutObject(saveFilePath, src, oss.ContentEncoding("UTF-8"))
	if err != nil {
		zap.L().Error("oss.UploadAliyunOss PutObject Error", zap.Error(err))
	}
	zap.L().Info("Upload Video to Oss Success!")

	//5.获取PlayUrl
	var remote string = "https://douyin54007.oss-cn-beijing.aliyuncs.com/"
	playUrl = remote + saveFilePath
	return
}

//客户端-->GO-->电脑
//客户端-->GO-->oss
